module GiteeTransfer
  class Worker
    def execute(options = {})
      start_at = Time.now
      run(options[:page])
      end_at = Time.now

      GiteeTransfer.logger.info "Successed count: #{GiteeTransfer.counter.successed_count}"
      GiteeTransfer.logger.info "Failed count: #{GiteeTransfer.counter.failed_count}"
      GiteeTransfer.logger.info "Skipped count: #{GiteeTransfer.counter.skipped_count}"

      GiteeTransfer.logger.info "Successed project_ids: #{GiteeTransfer.counter.successed}"
      GiteeTransfer.logger.info "Failed project_ids: #{GiteeTransfer.counter.failed}"
      GiteeTransfer.logger.info "Skipped project_ids: #{GiteeTransfer.counter.skipped}"

      GiteeTransfer.logger.info "Complete! cost time: #{end_at - start_at} seconds."
    end

    private

    def run(page)
      page ||= 1
      GiteeTransfer.logger.info "[RUN_PAGE] Current page: #{page}"

      projects = Request.new.get_projects(page: page)
      return '获取仓库列表失败' if projects.nil?

      return '迁移完毕' if projects.empty?

      projects.each do |project|
        unless Namespace.get_namespace_id(project['namespace']['path'])
          GiteeTransfer.counter.skipped << project['id']
          next
        end

        result = transfer_repo(project)
        if result[:status]
          GiteeTransfer.counter.successed << project['id']
          GiteeTransfer.logger.info "project_id: #{project['id']} -- #{result[:message]}"
        else
          GiteeTransfer.counter.failed << project['id']
          GiteeTransfer.logger.error "[RUN_ERROR] project_id: #{project['id']} -- #{result[:message]}"
        end
      end

      page += 1
      run(page)
    end

    def transfer_repo(project)
      # Clone repo
      result = Git.new.clone(
        # repo_url: project['html_url'],
        repo_url: project['ssh_url'],
        namespace: project['namespace']['path']
      )
      return {status: false, message: 'Clone repo failed'} unless result

      # Create repo
      new_project = Request.new.create_project(
        program_id: Namespace.get_namespace_id(project['namespace']['path']),
        language: Language.get_language_id(project['language']),
        name: project['name'],
        path: project['path'],
        description: "#{project['description']}---#{project['id']}",
        can_create_branch: project['can_create_branch'],
      )
      return {status: false, message: 'Created Repo Failed'} unless new_project

      # Push repo
      result = Git.new.push(
        repo_url: "#{GiteeTransfer.config.xly_git_host}/#{new_project['project']['path_with_namespace']}.git",
        namespace: project['namespace']['path']
      )
      return {status: false, message: 'Push repo failed'} unless result

      # Set default branch
      if project['default_branch']
        result = Request.new.set_default_branch(
          namespace: new_project['project']['namespace_path'],
          repo_path: new_project['project']['path'],
          branch: project['default_branch']
        )
        return {status: false, message: 'Set default branch failed'} unless result
      end

      {status: true, message: 'Transfer Success'}
    end
  end
end
